home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp95 / gnuchess.arc / cvt.c < prev    next >
C/C++ Source or Header  |  1992-01-16  |  2KB  |  105 lines

  1. # include    <stdio.h>
  2.  
  3. # define    SET_BIT(b)    bits[i >> 3] |= (1 << (7 - (b & 7)))
  4.  
  5. int    line_no = 0;
  6. char    lbuf[BUFSIZ];
  7. char    bits[1024];
  8. FILE    *fp = stdin;
  9. int    height;
  10. int    max_size;
  11.  
  12. main(argc, argv)
  13. int    argc;
  14. char    **argv;
  15. {
  16.  
  17.     /***********************************************/
  18.     /*   Read  the  header to tell us how big the  */
  19.     /*   bitmaps should be.                   */
  20.     /***********************************************/
  21.     while (!feof(fp)) {
  22.         if (fgets(lbuf, sizeof lbuf - 1, fp) == NULL)
  23.             break;
  24.         line_no++;
  25.         if (*lbuf == '#' || *lbuf == '\n')
  26.             continue;
  27.         if (*lbuf == '=')
  28.             break;
  29.         if (strncmp(lbuf, "height=", 7) == 0) {
  30.             height = atoi(lbuf + 7);
  31.             continue;
  32.             }
  33.         }
  34.     if (height == 0) {
  35.         fprintf(stderr, "height not defined in bitmap file\n");
  36.         exit(1);
  37.         }
  38.  
  39.     while (!feof(fp))
  40.         read_glyph();
  41.     printf("/* max buffer size=%d bytes */\n", max_size);
  42. }
  43. read_glyph()
  44. {
  45.     char    *cp;
  46.     int    i;
  47.     int    r = 0;
  48.     int    c = 0;
  49.     int    w = 0;
  50.  
  51.     memset(bits, 0, sizeof bits);
  52.  
  53.     while (*lbuf != '=') {
  54.         if (fgets(lbuf, sizeof lbuf - 1, fp) == NULL)
  55.             return;
  56.         line_no++;
  57.         }
  58.     for (cp = lbuf; *cp == '='; )
  59.         cp++;
  60.     printf("\t/*");
  61.     while (*cp != '=')
  62.         printf("%c", *cp++);
  63.     printf("*/\n");
  64.     while (1) {
  65.         if (fgets(lbuf, sizeof lbuf - 1, fp) == NULL)
  66.             break;
  67.         line_no++;
  68.         if (*lbuf == '=' || *lbuf == '\n')
  69.             break;
  70.         cp = lbuf;
  71.         if (*cp++ != '\t') {
  72.             fprintf(stderr, "Line %d: missing tab character\n", line_no);
  73.             exit(1);
  74.             }
  75.         if (*cp == '|')
  76.             cp++;
  77.         for (c = 0; *cp != '\n' && *cp != '|'; c++) {
  78.             if (*cp++ != ' ') {
  79.                 i = r * ((w + 7) & ~7) + c;
  80.                 SET_BIT(i);
  81.                 }
  82.             }
  83.         if (c > w)
  84.             w = c;
  85.         r++;
  86.         }
  87.     if (r == 0)
  88.         return;
  89.     if (r != height) {
  90.         fprintf(stderr, "Line %d: Glyph has %d rows out of %d\n", r, height);
  91.         }
  92.     printf("\t{1, 1, %d, %d,\n\t", w, height);
  93.     w = (w + 7) & ~7;
  94.     if (height * w / 8 > max_size)
  95.         max_size = height * w / 8;
  96.     for (i = 0; i < height * w / 8; i++) {
  97.         if (i)
  98.             printf(", ");
  99.         if ((i & 7) == 7)
  100.             printf("\n\t");
  101.         printf("0x%02x", bits[i] & 0xff);
  102.         }
  103.     printf("},\n");
  104. }
  105.